﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using K.Presentation;
using K.EnterpriseLibary.Entity;
using System.Data;
namespace CreateDictionaryWeb
{
    public partial class MaintainCharacterInfo : System.Web.UI.Page
    {
        private ChineseDictAction chineseDictAction = new ChineseDictAction();

        private Character CharacterForPage
        {
            get
            {
                return ViewState["CharacterForPage"] as Character;
            }
            set
            {
                ViewState["CharacterForPage"] = value;
            }
        }

        private List<CharacterInfo> ListCharacterInfoForPage
        {
            get
            {
                return ViewState["ListCharacterInfoForPage"] as List<CharacterInfo>;
            }
            set
            {
                ViewState["ListCharacterInfoForPage"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btUpdateCharacter_Click(object sender, EventArgs e)
        {
            //更新汉字
            chineseDictAction.UpdateCharacter(ccFirstCharacter.Character); 
        }

        protected void btGetACharacter_Click(object sender, EventArgs e)
        {
            //获取未维护的汉字
            Character character = new Character();
            character = chineseDictAction.GetRandomCharacter();
            ccFirstCharacter.Character = character;
            cicCharacterInfo.Character = character;
            ViewState["CharacterForPage"] = character;
            ShowCharacterInfoList();
        }

        private void ShowCharacterInfoList()
        {
            //显示已经维护好的意义
            List<CharacterInfo> listCharacterInfo = new List<CharacterInfo>();
            listCharacterInfo = chineseDictAction.GetCharacterInfoList(ViewState["CharacterForPage"] as Character);
            GVCharacterInfo.DataSource = listCharacterInfo;
            GVCharacterInfo.DataBind();
            ViewState["ListCharacterInfoForPage"] = listCharacterInfo;
        }

        protected void btSaveCharacterInfo_Click(object sender, EventArgs e)
        {
            Character c = cicCharacterInfo.CharacterInfo.Character;
            chineseDictAction.SaveCharacterInfo(cicCharacterInfo.CharacterInfo);
            //if (cicCharacterInfo.ListCharacterInfoProvenance.Count == 1 && cicCharacterInfo.ListCharacterInfoProvenance[0].Content == "" && cicCharacterInfo.ListCharacterInfoProvenance[0].AurthName == "" && cicCharacterInfo.ListCharacterInfoProvenance[0].ArticelName == "")
            //    return;
            cicCharacterInfo.GetCharacterInfoList();
            chineseDictAction.SaveCharacterInfoProvenance(cicCharacterInfo.ListCharacterInfoProvenance);
            ShowCharacterInfoList();
        }

        protected void btEditCharacterInfo_Click(object sender, EventArgs e)
        {

        }

        protected void GVCharacterInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            List<CharacterInfo> listCharacterInfo = new List<CharacterInfo>();
            listCharacterInfo = ViewState["ListCharacterInfoForPage"] as List<CharacterInfo>;
            List<CharacterInfoProvenance> listCharacterInfoProvencance = new List<CharacterInfoProvenance>();
            //ListCharacterInfoForPage = chineseDictAction.g
            //foreach (var item in listCharacterInfo[e.RowIndex])
            //{
                
            //}
            chineseDictAction.DeleteCharacterInfo(listCharacterInfo[e.RowIndex]);
            listCharacterInfo.Remove(listCharacterInfo[e.RowIndex]);
            //chineseDictAction.DeleteCharacterInfoProvenance(listCharacterInfo[e.RowIndex].);

            GVCharacterInfo.DataSource = listCharacterInfo;
            GVCharacterInfo.DataBind();
        }

        protected void btSaveAll_Click(object sender, EventArgs e)
        {
            ccFirstCharacter.Character.CompleteFlag = 1;
        }
    }

}